package com.yidu.t299.dao.lzy;

import com.yidu.t299.domain.lzy.Market;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

@Mapper
public interface MarketaMapper {
    /**
     * 根据计划编号删除该市场计划信息
     * @param planId
     * @return 影响的行数(返回1则为成功)
     */
    @Delete({
            "delete from market" +
                    " where plan_id = #{planId,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer planId);

    /**
     * 添加校区信息
     * @param market 校区对象
     * @return 影响的行数(返回1则为成功)
     */
    @Insert({
            "insert into market (plan_id, plan_mgr,plan_name,plan_begin_date,plan_end_date,source_id,plan_body,total_money,custom_count,execute_state)" +
                    " values (#{planId,jdbcType=INTEGER},#{planMgr,jdbcType=INTEGER}," +
                    "#{planName,jdbcType=VARCHAR},#{planBeginDate,jdbcType=VARCHAR}," +
                    "#{planEndDate,jdbcType=VARCHAR},#{sourceI" +
                    "·d,jdbcType=INTEGER}," +
                    "#{planBody,jdbcType=VARCHAR},#{totalMoney,jdbcType=INTEGER}," +
                    "#{customCount,jdbcType=INTEGER},#{executeState,jdbcType=VARCHAR})"
    })
    int insert(Market market);

    /**
     * 根据校区编号查询指定的信息
     * @param planId 校区编号
     * @return 校区对象
     */
    @Select({
            "select" +
                    " plan_id, plan_mgr,plan_name,plan_begin_date,plan_end_date," +
                    "source_id,plan_body,total_money,custom_count,execute_state" +
                    " from market" +
                    " where plan_id = #{planId,jdbcType=INTEGER}"
    })
    @Results(id = "MarketMap",value = {
            @Result(id = true,column = "plan_id",property = "planId"),
            @Result(column = "plan_mgr",property = "planMgr"),
            @Result(column = "plan_name",property = "planName"),
            @Result(column = "plan_begin_date",property = "planBeginDate"),
            @Result(column = "plan_end_date",property = "planEndDate"),
            @Result(column = "source_id",property = "sourceId"),
            @Result(column = "plan_body",property = "planBody"),
            @Result(column = "total_money",property = "totalMoney"),
            @Result(column = "custom_count",property = "customCount"),
            @Result(column = "execute_state",property = "executeState")
    })
    Market selectByPrimaryKey(Integer planId);

    /**
     * 按条件分页查询校区数据集合
     * @param rows 每页的行数
     * @param page 页码
     * @param condition 条件参数
     * @return 校区对象集合
     */
    @Select("SELECT * " +
            "FROM market m " +
            "JOIN source_setting ss " +
            "ON m.source_id=ss.source_id " +
            "where 1=1 ${condition} limit #{page},#{rows}")
    @Results(id="MarketJoinMap",value={
            @Result(column = "plan_id", property = "planId", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "plan_mgr", property = "planMgr", jdbcType = JdbcType.INTEGER),
            @Result(column = "plan_name", property = "planName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "plan_begin_date", property = "planBeginDate", jdbcType = JdbcType.VARCHAR),
            @Result(column = "plan_end_date", property = "planEndDate", jdbcType = JdbcType.VARCHAR),
            @Result(column = "source_id", property = "sourceId", jdbcType = JdbcType.INTEGER),
            @Result(column = "plan_body", property = "planBody", jdbcType = JdbcType.VARCHAR),
            @Result(column = "total_money", property = "totalMoney", jdbcType = JdbcType.INTEGER),
            @Result(column = "custom_count", property = "customCount", jdbcType = JdbcType.INTEGER),
            @Result(column = "execute_state", property = "executeState", jdbcType = JdbcType.VARCHAR),
            @Result(column = "source_name", property = "sourceName", jdbcType = JdbcType.VARCHAR),
    })
    List<Market> selectByPage(@Param("rows") int rows, @Param("page") int page, @Param("condition") String condition);

    /**
     * 按条件查询school表中数据行数
     * @param condition 条件参数
     * @return 数据行数
     */
    @Select("SELECT count(*) " +
            "FROM market m " +
            "JOIN source_setting ss " +
            "ON m.source_id=ss.source_id " +
            "where 1=1 ${condition}")
    int selectCount(@Param("condition") String condition);

    /**
     * 根据校区信息修改其数据
     * @param market 校区对象
     * @return 影响的行数(返回1则为成功)
     */
    @Update({
            "update market",
            " set plan_mgr = #{planMgr,jdbcType=INTEGER}," +
              "plan_name = #{planName,jdbcType=VARCHAR}," +
            "plan_begin_date = #{planBeginDate,jdbcType=VARCHAR}," +
            "plan_end_date = #{planEndDate,jdbcType=VARCHAR}," +
             "source_id = #{sourceId,jdbcType=INTEGER}," +
             "plan_body = #{planBody,jdbcType=VARCHAR}," +
             "total_money = #{totalMoney,jdbcType=INTEGER}," +
             "custom_count = #{customCount,jdbcType=INTEGER}," +
             "execute_state = #{executeState,jdbcType=VARCHAR}",

            " where plan_id = #{planId,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(Market market);




    @Select("SELECT source_id FROM source_setting WHERE source_name=#{sourceName}")
    String selectBySourceName(String sourceName);



}
